package com.cc.crm.web;

import com.cc.crm.service.IReptService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/rept")
public class ReptController {
    @Autowired
    private IReptService iReptService;

    @RequestMapping("/cons")
    public String consIndex() {
        //1.调用service查询客户构成的统计
        //2.绑定数据到model
        //3.跳转到cons.jsp绑定数据
        return "~rept/cons";
    }

    @RequestMapping("/consPie")
    public void createConsPie(HttpServletResponse response) throws IOException {
        //得到响应的输出流对象，通过输出流向网页输出一张图片
        OutputStream os = response.getOutputStream();
        //将jfreechart生成的图片信息写入到os中就行了
        iReptService.writeJFreeChartToOsCons(os);
    }

    @RequestMapping("/downloadcontr")
    public void downloadcontr(HttpServletResponse response) throws IOException {
        //1.调用Service获取用户组成的分析集合，这里用模拟数据
        List<Person> list = Arrays.asList(new Person("码云", 100 * 100),
                new Person("马化腾", 1000 * 100));
        //2.excel文件对象
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle cellStyle = wb.createCellStyle();

        cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中

        //3.根据excel文件创建一个sheet对象
        Sheet sheet = wb.createSheet("users");//创建一张表
        //4.为sheet添加第1行t数据
        Row topRow = sheet.createRow(0);//创建第一行，起始为0
        Cell cell1 = topRow.createCell(0);
        cell1.setCellStyle(cellStyle);
        cell1.setCellValue("序号");


        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 5);
        sheet.addMergedRegion(region);
//        sheet.setHorizontallyCenter(true);
        Row titleRow = sheet.createRow(1);//创建第一行，起始为0
        titleRow.createCell(0).setCellValue("序号");
        titleRow.createCell(1).setCellValue("姓名");//第一列
        titleRow.createCell(2).setCellValue("公司");
        titleRow.createCell(3).setCellValue("添加人");
        titleRow.createCell(4).setCellValue("邮箱");
        titleRow.createCell(5).setCellValue("电话");
        //5.为sheet添加后续行
        int cell = 2;
        for (Person riskUser : list) {
            Row row = sheet.createRow(cell);//从第二行开始保存数据
            row.createCell(0).setCellValue(cell);
            row.createCell(1).setCellValue(riskUser.getName());//将数据库的数据遍历出来
            row.createCell(2).setCellValue(riskUser.getCount());
            cell++;
        }
        //6.设置Excel文件的名字
        String fileName = "user用户信息报表.xlsx";
        OutputStream outputStream = null;
        try {
            //7.下载的文件的名字的编码为utf-8
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式  响应的内容格式
            //8.输出数据类型--mima类型：参考：https://www.w3school.com.cn/media/media_mimeref.asp
            response.setContentType("application/vnd.ms-excel");
            //9.控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名，文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
            //参考：https://www.cnblogs.com/wq-9/articles/12165056.html
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            //10、通过response输出流输出excel对象
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class Person {
        private String name;
        private int count;

        public Person(String name, int count) {
            this.name = name;
            this.count = count;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }


    }
}

